package zoo.qboard.user.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import zoo.qboard.dao.QboardDao;
import zoo.qboard.domain.QboardCommand;
import zoo.qboard.validator.QboardValidator;

@Controller
public class QboardInsertController {

	private QboardDao qboardDao;
	private String formViewName = "qboardinsert";
	@Autowired
	public void setMemberDao(QboardDao qboardDao) {
		this.qboardDao = qboardDao;
	}

	@RequestMapping(value="/main/qboardinsert.do", method=RequestMethod.GET)
	public String form(){
		return formViewName;
	}

	@ModelAttribute
	public QboardCommand formBacking() {
		return new QboardCommand();
	}

	@RequestMapping(value="/main/qboardinsert.do", method=RequestMethod.POST)
	public String submit(QboardCommand qboardCommand, BindingResult result){
		new QboardValidator().validate(qboardCommand, result);

		if(result.hasErrors()){
			return formViewName;
		}

		qboardDao.insertQboard(qboardCommand);

		return "redirect:/main/qboardlist.do";
	}
	
	@ModelAttribute("qnaTypes")
	protected List<String> referenceData2() throws Exception{
		List<String> qnaTypes =new ArrayList<String>();
		
		qnaTypes.add("문의선택");
		qnaTypes.add("주문관련");
		qnaTypes.add("배송관련");
		qnaTypes.add("포인트관련");
		
		return qnaTypes;
	}
}